package com.pactera.asmp.server.utils;

import com.pactera.asmp.server.common.exception.GenericException;
import com.pactera.asmp.server.error.CommonValidationError;
import org.springframework.util.StringUtils;

import java.util.function.Function;

public final class AssertUtil {
    private AssertUtil() {
        throw new AssertionError();
    }


    public static void hasText(CharSequence input, String message) {
        hasText(input, message, null);
    }

    public static void hasText(CharSequence input, String message, Function<String, GenericException> exceptionSupplier) {
        if (!StringUtils.hasText(input)) {
            error(message, exceptionSupplier);
        }
    }

    public static void notNull(Object object, String message, Function<String, GenericException> exceptionSupplier) {
        if (object == null) {
            error(message, exceptionSupplier);
        }
    }

    public static void notNull(Object object, String message) {
        notNull(object, message, null);
    }

    public static void isTrue(boolean condition, String message) {
        isTrue(condition, message, null);
    }

    public static void isTrue(boolean condition, String message, Function<String, GenericException> exceptionSupplier) {
        if (!condition) {
            error(message, exceptionSupplier);
        }
    }

    private static void error(String message, Function<String, GenericException> exceptionSupplier) {
        if (exceptionSupplier != null) {
            throw exceptionSupplier.apply(message);
        }
        throw new GenericException(CommonValidationError.BASIC_BAD_REQUEST_PARAMS,message);
    }
}